#include "lab2.h"

// see lab2.h
UI::UI() {
    this->finished = false;
    this->debug = false;
}

// see lab2.h
UI::~UI() {
    
}

// see lab2.h
bool UI::isFinished() {
    return this->finished;
}

// see lab2.h
void UI::promptForInput() {
    while (!this->isFinished()) {
        if (displayMenu()) {
            promptForYear();
            promptForMonth();
            promptForJanFirst();
            
            Year::Year* year = new Year::Year(inputYear, janFirst);
            
            if (this->debug) {
                // Display all the months for debugging
                for (int i=0; i < 12; i++) {
                    Month::Month* month = year->getMonth(i);
                    month->printMonth();
                }
            } else {
                // Display only the specific month as required by assignment
                Month::Month* month = year->getMonth(this->inputMonth-1);
                cout << "For year " << this->inputYear << " and month " << this->inputMonth;
                cout << " starting on the " << this->janFirst << " day of the week";
                month->printMonth();                
            }

            // Exit the loop
            delete year;
            this->finished = true;
        }
    }
}

// see lab2.h
bool UI::displayMenu() {
    bool done = false;
    while (!done) {
        // Loop while we don't get either a Q or C
        cout << "\t(C) Create a calendar\n\t(Q) Quit Program.\n";
        cout << "  Your choice: ";
        
        string choice;
        getline(cin, choice);
        char selected = -1;
        if (choice.length() > 0) { selected = choice.at(0); }        
        switch (selected) {
            case 'Q':
            case 'q':
                this->finished = true;
                done = true;
                break;
            case 'C':
            case 'c':
                done = true;
                break;
            default:
                continue;
        }
    }
    return !this->finished;
}

// see lab2.h
void UI::promptForYear() {
    bool done = false;
    bool valid = false;
    while (!done) {
        cout << "\nInput a year between 1 and 9999: ";
        string choice;
        getline(cin, choice);
        if (choice.length() >0) {
            int tmp = atoi(choice.c_str());
            if (tmp > 0 && tmp < 10000) {
                inputYear = tmp;
                valid = true;
                done = true;
            }
        } 
        
        if (!valid) {
            cout << "\nInvalid year input.  Please try again.\n";
        }
    }
}

// see lab2.h
void UI::promptForMonth() {
    bool done = false;
    bool valid = false;
    while (!done) {
        cout << "\nInput a month from 1 to 12: ";
        string choice;
        getline(cin, choice);
        if (choice.length() >0) {
            int tmp = atoi(choice.c_str());
            if (tmp > 0 && tmp < 13) {
                inputMonth = tmp;
                valid = true;
                done = true;
            }
        } 
        
        if (!valid) {
            cout << "\nInvalid month input.  Please try again.\n";
        }
    }
}

// see lab2.h
void UI::promptForJanFirst() {
    bool done = false;
    bool valid = false;
    while (!done) {
        cout << "\nInput the day of the week that Jan 1st falls on.  Use the following characters:\n";
        cout << "\tM - Monday\n";
        cout << "\tT - Tuesday\n";
        cout << "\tW - Wednesday\n";
        cout << "\tR - Thursday\n";
        cout << "\tF - Friday\n";
        cout << "\tS - Saturday\n";
        cout << "\tU - Sunday\n";
        
        string choice;
        getline(cin, choice);
        if (choice.length() >0) {
            switch (choice.c_str()[0]) {
                case 'M':
                case 'm':
                    janFirst = 2;
                    valid = done = true;
                    break;
                case 'T':
                case 't':
                    janFirst = 3;
                    valid = done = true;
                    break;
                case 'W':
                case 'w':
                    janFirst = 4;
                    valid = done = true;
                    break;
                case 'R':
                case 'r':
                    janFirst = 5;
                    valid = done = true;
                    break;
                case 'F':
                case 'f':
                    janFirst = 6;
                    valid = done = true;
                    break;
                case 'S':
                case 's':
                    janFirst = 7;
                    valid = done = true;                
                    break;
                case 'U':
                case 'u':
                    janFirst = 1;
                    valid = done = true;
                    break;
                default:
                    break;
            }
        } 

        if (!valid) {
            cout << "\nInvalid month input.  Please try again.\n";
        }
    }
}

// see lab2.h
void UI::setDebugFlag(bool isSet) {
    this->debug = isSet;
}

// see lab2.h
int main(int argc, char* argv[]) {
    UI::UI ui = UI::UI();
    
    // Check for debug flag
    if (argc >= 2) {
        std::string debug = argv[1];
        if (debug == "true") {
            ui.setDebugFlag(true);            
        }
    }
    
    // Loop while waiting for the user to exit, or for us to get a command break
    while (!ui.isFinished()) {
        ui.promptForInput();
    }
}